//
//  SignTopView.m
//  BOSS-SOOC
//
//  Created by houshijia on 16/9/5.
//  Copyright © 2016年 Gcx. All rights reserved.
//

#import "SignTopView.h"

@implementation SignTopView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)initWithStyle:(SignTopStyle)style{
    if (self = [super init]) {
        
        self.frame = CGRectMake(0, 0, SIZEWIDTH, ScaleSize(156));
        self.backgroundColor = BGC;
        self.myStyle = style;
    }
    
    return self;
}

#pragma mark- setMyStyle
-(void)setMyStyle:(SignTopStyle)myStyle{
    switch (myStyle) {
        case 1:
            [self createSearchAndCode];
            break;
        case 2:
            [self createSearchOnly];
            break;
        default:
            break;
    }
}
#pragma mark- CreateUI
-(void)createSearchAndCode{
    [self addSubview:self.containView];
    
    self.searchBar.frame = CGRectMake(0, 0, SIZEWIDTH-100, ScaleSize(96));
    self.searchBar.placeholder = @"课堂编号/教师名称";
    [self.searchBar addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.containView addSubview:self.searchBar];
    
    self.searchBtn.frame = CGRectMake(self.containView.frame.size.width-36-ScaleSize(99), (ScaleSize(96)-18)/2, 18, 18);
    [self.searchBtn addTarget:self action:@selector(clickButtonSearch:) forControlEvents:UIControlEventTouchUpInside];
    
    self.codeBtn.frame = CGRectMake(self.containView.frame.size.width-18-ScaleSize(33), (ScaleSize(96)-18)/2, 18, 18);
    [self.codeBtn addTarget:self action:@selector(clickButtonScan:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.containView addSubview:self.searchBtn];
    [self.containView addSubview:self.codeBtn];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(self.containView.frame.size.width-18-ScaleSize(66), (ScaleSize(96)-18)/2-1, 0.5, 21)];
    line.backgroundColor = colordbdbdb;
    [self.containView addSubview:line];
    
}

-(void)createSearchOnly{
    [self addSubview:self.containView];
    
    self.searchBar.frame = CGRectMake(0, 0, SIZEWIDTH-70-6, ScaleSize(96));
    self.searchBar.placeholder = @"课程名称/教师名称";
    [self.containView addSubview:self.searchBar];

    self.searchBtn.frame = CGRectMake(self.containView.frame.size.width-18-ScaleSize(33), (ScaleSize(96)-18)/2, 18, 18);

    [self.searchBtn addTarget:self action:@selector(clickButtonSearch:) forControlEvents:UIControlEventTouchUpInside];
    [self.containView addSubview:self.searchBtn];
    
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(self.containView.frame.size.width-18-ScaleSize(66), (ScaleSize(96)-18)/2-1, 0.5, 21)];
    line.backgroundColor = colordbdbdb;
    [self.containView addSubview:line];
}





#pragma mark- 点击方法
/*
 输入框内容改变时
 */
-(void)textFieldDidChange:(UITextField*)textField{
    if (self.searchBar.text.length > 0) {

        self.searchBar.frame = CGRectMake(0, 0, SIZEWIDTH-100+10+ScaleSize(33), ScaleSize(96));
        
       self.searchBtn.frame = CGRectZero;
        [self.codeBtn setImage:[UIImage imageNamed:@"搜索框内icon"] forState:UIControlStateNormal];
        [self.codeBtn removeTarget:self action:@selector(clickButtonScan:) forControlEvents:UIControlEventTouchUpInside];
        [self.codeBtn addTarget:self action:@selector(clickButtonSearch:) forControlEvents:UIControlEventTouchUpInside];
        
    } else {
        self.searchBar.frame = CGRectMake(0, 0, SIZEWIDTH-100, ScaleSize(96));
        
        self.searchBtn.frame = CGRectMake(self.containView.frame.size.width-36-ScaleSize(99), (ScaleSize(96)-18)/2, 18, 18);
        
        [self.codeBtn setImage:[UIImage imageNamed:@"搜索框内扫码"] forState:UIControlStateNormal];
        [self.codeBtn removeTarget:self action:@selector(clickButtonSearch:) forControlEvents:UIControlEventTouchUpInside];
        [self.codeBtn addTarget:self action:@selector(clickButtonScan:) forControlEvents:UIControlEventTouchUpInside];
        
    }

}
/*
 今日签到搜索按钮点击事件
 */
-(void)clickButtonSearch:(UIButton*)sender{
//    [_searchBarToday resignFirstResponder];
//    
//    [_todayArr removeAllObjects];
//    _pageT = 1;
//    [self loadMySignData:@"2" andPage:1];
    if([self.delegate respondsToSelector:@selector(searchTodaySignClick)]){
        [self.delegate searchTodaySignClick];
    }
    
}
/*
 二维码按钮点击事件
 */
-(void)clickButtonScan:(UIButton*)sender{
    //二维码
    //    [[NSNotificationCenter defaultCenter] postNotificationName:@"openScan" object:nil];
    
//    SubLBXScanViewController *vc = [SubLBXScanViewController new];
//    vc.delegate = self;
//    [self.navigationController pushViewController:vc animated:YES];
    if ([self.delegate respondsToSelector:@selector(codeButtonClick)]) {
        [self.delegate codeButtonClick];
    }
    
    
}

/*
 历史签到按钮点击事件
 */
-(void)clickSearchButton:(UIButton*)sender{
    if ([self.delegate respondsToSelector:@selector(searchHistorySignClick)]) {
        [self.delegate searchHistorySignClick];
    }
}

#pragma mark- 懒加载控件
-(UIView *)containView{
    if (!_containView) {
        _containView = [[UIView alloc]initWithFrame:CGRectMake(ScaleSize(33), ScaleSize(30), SIZEWIDTH-ScaleSize(66), ScaleSize(99))];
    }
    _containView.layer.borderWidth = 0.5;
    _containView.layer.borderColor = colordbdbdb.CGColor;
    _containView.layer.cornerRadius = 2;
    _containView.backgroundColor = [UIColor whiteColor];
    
    return _containView;
}

-(UITextField *)searchBar{
    if (!_searchBar) {
        _searchBar = [[SC_TextFeild alloc]init];
    }
    [_searchBar setValue:colordbdbdb forKeyPath:@"_placeholderLabel.textColor"];
    [_searchBar  setValue:NormalFont(42) forKeyPath:@"_placeholderLabel.font"];
    _searchBar.font = NormalFont(42);
    
    _searchBar .keyboardType = UIKeyboardTypeDefault;
    _searchBar.enablesReturnKeyAutomatically = YES;
    _searchBar.tag = 10001;
    _searchBar .borderStyle = UITextBorderStyleNone;
    _searchBar.backgroundColor = [UIColor whiteColor];
    _searchBar.clearButtonMode = UITextFieldViewModeWhileEditing;
    return _searchBar;
}

-(UIButton *)searchBtn{
    if (!_searchBtn) {
        _searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_searchBtn setImage:[UIImage imageNamed:@"搜索框内icon"] forState:UIControlStateNormal];
    }
   
    return _searchBtn;
}
-(UIButton *)codeBtn{
    if (!_codeBtn) {
        _codeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_codeBtn setImage:[UIImage imageNamed:@"搜索框内扫码"] forState:UIControlStateNormal];

    }
        return _codeBtn;
}



@end
